<template>
  <div class="news-wrap-page">
    <Header />
    <div class="news-wrap-list">
      <div class="news-title-wrap">
        <div class="news-details-title">
          {{ newsData.news_title_show }}
        </div>
        <div class="new-list-border"></div>
      </div>
      <div
        v-loading="loading"
        class="new-list"
        element-loading-text="Loading..."
        :element-loading-spinner="svg"
        element-loading-svg-view-box="-10, -10, 50, 50"
        element-loading-background="rgba(0,0,0, 0)"
      >
        <a
          v-for="v in newsData.list"
          :key="v.id"
          class="new-list-item flex-colunm"
          :href="`/news/detail?id=${v.id}`"
          target="_blank"
        >
          <el-image :src="v.cover" lazy />
          <div style="height: 106px">
            <div class="white fs-26 fw-600 ellipsis-multiline-2 mt20">
              {{ v.title }}
            </div>
            <div class="mt10 white fs-16 fw-500">
              {{ v.publish_date }}
            </div>
          </div>
        </a>
      </div>
      <div class="flex align-center space-center news-pagination">
        <el-pagination
          v-model:current-page="page"
          background
          layout="prev, pager, next"
          :default-page-size="limit"
          :total="total"
          @current-change="currentChange"
        />
      </div>
    </div>
    <Footer />
  </div>
</template>
<script setup>
import Footer from '@/components/Footer.vue'
import Header from '@/components/Header.vue'

const newsData = useState('news-data', () => {})

const { $apiFetch } = useNuxtApp()
const { state } = usePageData()
const page = ref(1)
const loading = ref(false)
const limit = 8
const { data, error } = await useAsyncData('news-list-data', () =>
  $apiFetch(`/news`, {
    method: 'GET',
    params: {
      lang: state.value.language,
      limit,
      page: page.value,
    },
  }),
)

console.log('data-------', data.value)
if (!error.value) {
  newsData.value = data.value.data
}

const total = computed(() => {
  return newsData.value.count
})

const getData = async () => {
  loading.value = true
  const res = await $apiFetch(`/news`, {
    method: 'GET',
    params: {
      lang: state.value.language,
      limit,
      page: page.value,
    },
  })
  loading.value = false
  newsData.value = res.data
}

const currentChange = () => {
  getData()
}
</script>
<style lang="scss" scoped>
.news-wrap-page {
  background: #171a26;
  .news-wrap-list {
    width: 1000px;
    margin: 0 auto;
  }
  .news-pagination {
    margin-top: 64px;
    margin-bottom: 100px;
  }
  .mt10 {
    margin-top: 10px;
  }
  .mt20 {
    margin-top: 20px;
  }
  .new-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    .el-image {
      width: 481px;
      height: 360px;
    }
    &-item {
      width: 481px;
      display: flex;
      margin-top: 50px;
      cursor: pointer;
    }
  }
  .news-title-wrap {
    margin-top: 30px;
    color: #fff;
    font-size: 24px;
  }
  .news-details-title {
    margin-left: 5px;
    font-weight: 800;
  }
  .new-list-border {
    width: 1000px;
    height: 1px;
    background: rgba(255, 255, 255, 0.6);
    margin-top: 12px;
    position: relative;

    &:after {
      content: '';
      display: block;
      width: 60px;
      height: 4px;
      background: #00b9ef;
      position: absolute;
      top: -2px;
    }
  }
}
</style>
